package com.sunday.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunday.common.utils.PageUtils;
import com.sunday.common.utils.Query;
import com.sunday.gulimall.product.dao.CategoryDao;
import com.sunday.gulimall.product.entity.CategoryBrandRelationEntity;
import com.sunday.gulimall.product.entity.CategoryEntity;
import com.sunday.gulimall.product.service.CategoryBrandRelationService;
import com.sunday.gulimall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        // 1. 获取所有数据
        List<CategoryEntity> entities = baseMapper.selectList(null);
        // 2.组装树形结构
        // 2.1 找到top
        List<CategoryEntity> collect = entities.stream().filter(categoryEntity ->
                categoryEntity.getParentCid() == 0
        ).map((menu) -> {
            menu.setChildren(getChildren(menu, entities));
            return menu;
        }).sorted((menu1, menu2) -> {
            return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());

        for (CategoryEntity entity : entities) {
            if (entity.getParentCid() == 0) {

            }
        }
        return collect;
    }

    @Override
    public void removeMenuById(List<Long> catIds) {
        baseMapper.deleteBatchIds(catIds);
    }

    @Override
    public Long[] setCatelogPath(Long catelogId) {
        List<Long> path = new ArrayList<>();
        List<Long> parentPath = getParentPath(catelogId, path);
        Collections.reverse(parentPath);
        return parentPath.toArray(new Long[parentPath.size()]);
    }

    @Override
    public void updateDetail(CategoryEntity category) {
        this.updateById(category);
        CategoryEntity categoryEntity = new CategoryEntity();
        categoryEntity.setCatId(category.getCatId());
        categoryEntity.setName(category.getName());
        categoryBrandRelationService.updateCategoryRelation(categoryEntity);

    }

    private List<Long> getParentPath(Long id, List<Long> path) {
        path.add(id);
        CategoryEntity entity = this.getById(id);
        if (entity.getParentCid() == 0) {
            return path;
        }
        return getParentPath(entity.getParentCid(), path);
    }

    private List<CategoryEntity> getChildren(CategoryEntity root, List<CategoryEntity> all) {

        List<CategoryEntity> children = all.stream().filter(categoryEntity ->
                categoryEntity.getParentCid() == root.getCatId()
        ).map(categoryEntity -> {
            categoryEntity.setChildren(getChildren(categoryEntity, all));
            return categoryEntity;
        }).sorted((menu1, menu2) -> {
            return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());

        return children;
    }

}